package cn.csc.module.distribute.controller.admin.audit;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.module.distribute.controller.admin.audit.vo.DistributorAuditCreateReqVO;
import cn.csc.module.distribute.controller.admin.audit.vo.DistributorAuditRespVO;
import cn.csc.module.distribute.controller.admin.audit.vo.DistributorAuditUpdateReqVO;
import cn.csc.module.distribute.convert.audit.DistributorAuditConvert;
import cn.csc.module.distribute.dal.dataobject.audit.DistributorAuditDO;
import cn.csc.module.distribute.service.audit.DistributorAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigInteger;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 分销员审核记录")
@RestController
@RequestMapping("/distribute/distributor-audit")
@Validated
public class DistributorAuditController {

    @Resource
    private DistributorAuditService distributorAuditService;

    /**
     * 创建分销员审核记录
     */
    @PostMapping("/create")
    @ApiOperation("创建分销员审核记录")
    @PreAuthorize("@ss.hasPermission('distribute:distributor-audit:create')")
    public CommonResult<BigInteger> createDistributorAudit(@Valid @RequestBody DistributorAuditCreateReqVO createReqVO) {
        return success(distributorAuditService.createDistributorAudit(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新分销员审核记录")
    @PreAuthorize("@ss.hasPermission('distribute:distributor-audit:update')")
    public CommonResult<Boolean> updateDistributorAudit(@Valid @RequestBody DistributorAuditUpdateReqVO updateReqVO) {
        distributorAuditService.updateDistributorAudit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除分销员审核记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:distributor-audit:delete')")
    public CommonResult<Boolean> deleteDistributorAudit(@RequestParam("id") BigInteger id) {
        distributorAuditService.deleteDistributorAudit(id);
        return success(true);
    }

    /**
     * 根据用户ID获得分销员审核记录列表
     */
    @GetMapping("/getByUserId")
    @ApiOperation("根据用户ID获得分销员审核记录列表")
    @ApiImplicitParam(name = "userId", value = "用户编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:distributor-audit:query')")
    public CommonResult<List<DistributorAuditRespVO>> getByUserId(@RequestParam("userId") BigInteger userId) {
        List<DistributorAuditDO> byUserId = distributorAuditService.getByUserId(userId);
        return success(DistributorAuditConvert.INSTANCE.convertList(byUserId));
    }





}
